/*
 *  PoolAllocatorTester.cpp
 *  Common UnitTest
 *
 *  Created by jiro on 12/04/02.
 *  Copyright 2012 jiro music. All rights reserved.
 *
 */

#include "PoolAllocatorTester.h"

#include "../../Common/DebugSystem/CustomAssert.h"

#include "../../Common/UtilitySystem.h"

namespace sc
{
	void PoolAllocatorTester::TestAll()
	{
		Test_PoolAllocator000();
		Test_PoolAllocator001();
		Test_PoolAllocator002();
		Test_PoolAllocator003();
	}

	void PoolAllocatorTester::Test_PoolAllocator000()
	{
		PoolAllocator< int >::Allocate( 0 );
		AssertEqual( 1, PoolAllocator< int >::GetInstanceCount() );
		
		PoolAllocator< int >::DeallocateAll();
		AssertEqual( 0, PoolAllocator< int >::GetInstanceCount() );
	}

	void PoolAllocatorTester::Test_PoolAllocator001()
	{
		int instanceCount = 10;
		PoolAllocator< int >::Allocate( instanceCount );
		for ( int i = 0; i < instanceCount; i++ )
		{
			int* value = PoolAllocator< int >::GetInstance();
			* value = i;
			PoolAllocator< int >::ReturnInstance( value );
		}
		
		int* value = PoolAllocator< int >::GetInstance();
		AssertEqual( * value, 9 );

		PoolAllocator< int >::DeallocateAll();
	}

	void PoolAllocatorTester::Test_PoolAllocator002()
	{
		const int instanceCount = 10;
		int* ptrs[ instanceCount ];
		PoolAllocator< int >::Allocate( instanceCount );
		for ( int i = 0; i < instanceCount; i++ )
		{
			ptrs[ i ] = PoolAllocator< int >::GetInstance();
			* ( ptrs[ i ] ) = i;
		}
		for ( int i = 0; i < instanceCount; i++ )
		{
			PoolAllocator< int >::ReturnInstance( ptrs[ i ] );
		}
		for ( int i = 0; i < instanceCount; i++ )
		{
			int* ptr = PoolAllocator< int >::GetInstance();
			AssertEqual( * ptr, 9 - i );
		}
		PoolAllocator< int >::DeallocateAll();
	}

	void PoolAllocatorTester::Test_PoolAllocator003()
	{
		const int instanceCount = 10;
		PoolAllocator< int >::Allocate( instanceCount );
		AssertEqual( instanceCount, PoolAllocator< int >::GetInstanceCount() );
		for ( int i = 0; i < instanceCount; i++ )
		{
			PoolAllocator< int >::GetInstance();
		}
		PoolAllocator< int >::GetInstance();
		AssertEqual( 2 * instanceCount, PoolAllocator< int >::GetInstanceCount() );
		PoolAllocator< int >::DeallocateAll();
	}

}